#pragma once
#include <string>
#include "Connection.hpp"
#include "Callback.hpp"
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "Log copy.hpp"
//负责获取连接，采用非阻塞方式获取

using namespace LogModule;
// std::function<void(Connection*)>;
static void Accepter(Connection* conn)
{
    errno = 0;
    while (true)
    {
        struct sockaddr_in client;
        socklen_t len = sizeof(client);
        int n = accept(conn->GetSocket(), (sockaddr*)&client, &len);
        if(n > 0)
        {
            SetNonBlock(n);
            Handler reader = std::bind(CallBack::reader, std::placeholders::_1);
            Handler writer = std::bind(CallBack::writer, std::placeholders::_1);
            Handler excepter = std::bind(CallBack::excepter, std::placeholders::_1);
            Connection* _conn = ConnectionFactor::BuildNormalConnection(n, reader, writer, excepter, EPOLLIN | EPOLLET, conn->_R);
            conn->_R->AddConnction(_conn);
        }
        else
        {
            if(EAGAIN == errno || EWOULDBLOCK == errno) break;
            else if(EINTR == errno) continue;
            else
            {
                logger(LogLevel::DEBUG, __FILE__, __LINE__) << "Add conn error:" << strerror(errno);
                break;
            }
        }
        logger(LogLevel::DEBUG, __FILE__, __LINE__) << "执行一次accept!";
    }

}